﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Outdoors.ViewModels;
using Windows.Devices.Geolocation;
using Outdoors.Helpers.Foursquare;
using System.Collections.ObjectModel;

namespace Outdoors
{
    public partial class MainPageActivities : PhoneApplicationPage
    {
        private double Latitude { get; set; }
        private double Longitude { get; set; }
        public MainPageActivities()
        {
            InitializeComponent();

            Loaded += MainPageActivities_Loaded;
        }

        private async void MainPageActivities_Loaded(object sender, RoutedEventArgs e)
        {
            
            Geolocator geolocator = new Geolocator();
            geolocator.DesiredAccuracyInMeters = 50;

            Geoposition geoposition = await geolocator.GetGeopositionAsync(TimeSpan.FromMinutes(1), TimeSpan.FromSeconds(30));
            Latitude = geoposition.Coordinate.Latitude;
            Longitude = geoposition.Coordinate.Longitude;

            var activityViewModel = new ActivityViewModel(Latitude, Longitude);
            activityViewModel.LoadData();

            DataContext = activityViewModel;

        }

        private void ActivitiesList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var selector = sender as LongListSelector;

            if (selector == null)
                return;

            var selectedItem = selector.SelectedItem as ActivityItemViewModel;

            string navigateTo = string.Format("/MainPage.xaml?&latitude={0}&longitude={1}&categoryId={2}", Latitude, Longitude, selectedItem.CategoryId);
            NavigationService.Navigate(new Uri(navigateTo, UriKind.RelativeOrAbsolute));
        }

    }
}